---
title: "Wager and Britt Replication Code: The Racialized Impacts of Confederate Symbols in Public Spaces"
output: html_document
date: "2025-06-10"
---

library(tables)
library(Hmisc)
library(scales)
library(table1)
library(kableExtra)
library(gridExtra)
library(stargazer)
library(ggplot2)

```{r setup, include=FALSE}
rm(list = ls())
setwd("~/Dropbox/Monuments Project/3. Conf Mon Paper - Emily Lucy/Data/Data for publication")
df <- read.csv("data_dv.csv")
```


# Table: Open Courts ATE

```{r, warning=FALSE}

covariates <- "Southern.Identity_rescaled  + Age + Gender + State_name + Party_ID + Region +
Education + rr_index_rescaled + Citizen + community_confederate"

#H1a and H1b
m1 <- lm(as.formula(paste("open_court_scale_rescaled ~ condition +", covariates)), data = subset(df, Race == "2"),  na.action = na.exclude)
m2 <- lm(as.formula(paste("open_court_scale_rescaled ~ condition +", covariates)), data = subset(df, Race == "1"),  na.action = na.exclude)
m3 <- lm(as.formula(paste("open_court_scale_rescaled ~ condition +", covariates)), data = subset(df, Race == "3"),  na.action = na.exclude)


# Table 3:  not showing covariates
stargazer(list(m1, m2, m3), 
          omit = c("Southern.Identity_rescaled", "Age", "State_name","Party_ID", "Education", "rr_index_rescaled", "Citizen","Region", "Gender","community_confederate"),
          covariate.labels = c("Treatment", "Constant"),
          column.labels = c("White", "Black", "Latino"),
          omit.stat = c("rsq", "f"), 
          df = FALSE, 
          align = TRUE, 
          digits = 2, 
          no.space = TRUE,
          title = "Average Treatment Effect by Race on Perceptions of Open Court")


# Table 10: showing covariates (Appendix)
stargazer(list(m1, m2, m3), 
          covariate.labels = c("Treatment", "Southern Identity", "Age", "Male", "Other Gender",
                               "State: AR", "State: FL", "State: GA", "State: LA",
                               "State: MS", "State: NC", "State: SC", "State: TN",
                               "State: TX", "State: VA", "Party ID",
                               "Rural", "Small Suburb", "Small Town", "Urban",
                               "Education", "Racial Resentment Index",
                               "Naturalized Citizen", "Non-Citizen",
                               "Confederate Monument in Community", "Constant"),
          column.labels = c("White", "Black", "Latino"),
          omit.stat = c("rsq", "f"), 
          df = FALSE, 
          align = TRUE, 
          digits = 2, 
          no.space = TRUE,
          title = "Average Treatment Effect by Race on Perceptions of Open Court: Controls",
          out = "~/Dropbox/Apps/Overleaf/Confederate Monuments/table1_controls.tex")

```

# Table: Compare Racial Groups ATE

```{r, warning=FALSE}

m_interaction <- lm(as.formula(paste("open_court_scale_rescaled ~ condition* Race_full +", covariates)), data = subset(df, Race_full %in% c("Black", "Latino")))

m_interaction2 <- lm(as.formula(paste("open_court_scale_rescaled ~ condition* Race_full +", covariates)), data = subset(df, Race_full %in% c("Latino", "White")))

m_interaction3 <- lm(as.formula(paste("open_court_scale_rescaled ~ condition* Race_full +", covariates)), data = subset(df, Race_full %in% c("Black", "White")))
   

# Table 4:  not showing covariates 
stargazer(list(m_interaction,m_interaction2, m_interaction3), 
             omit = c("Southern.Identity_rescaled", "Age", "State_name","Party_ID", "Education", "rr_index_rescaled", "Citizen","Region", "Gender","community_confederate"),
         # omit.labels = c("Southern Identity", "State fixed effects", "Age"),
          covariate.labels = c("Treatment", "Latino","White", "Treatment x Latino", "Treatment x White", "Constant"),
          omit.stat = c("rsq", "f"), 
          df = FALSE, 
          align = TRUE, 
          digits = 2, 
          no.space = TRUE,
          title = "Effect of Confederate Monument in Front of Courthouse by Race"
)

# Table 11: showing covariates (Appendix)    
stargazer(list(m_interaction,m_interaction2, m_interaction3), 
          covariate.labels = c("Treatment", "Latino", "White", 
          "Southern Identity", "Age", "Male", "Other Gender",
                               "State: AR", "State: FL", "State: GA", "State: LA",
                               "State: MS", "State: NC", "State: SC", "State: TN",
                               "State: TX", "State: VA", "Party ID",
                               "Rural", "Small Suburb", "Small Town", "Urban",
                               "Education", "Racial Resentment Index",
                               "Naturalized Citizen", "Non-Citizen",
                               "Confederate Monument in Community", "Treatment x Latino", "Treatment x White", "Constant"),
          column.labels = c("Latino v Black", "Latino v White", "Black v White"),
          omit.stat = c("rsq", "f"), 
          df = FALSE, 
          align = TRUE, 
          digits = 2, 
          no.space = TRUE,
          title = "Effect of Confederate Monument in Front of Courthouse by Race: Controls",
          out = "~/Dropbox/Apps/Overleaf/Confederate Monuments/table2_controls.tex")

```

# Table: Proc Justice ATE

```{r, warning=FALSE}

m4 <- lm(as.formula(paste("tplm_rescaled ~ condition +", covariates)), data = subset(df, Race == "2"),  na.action = na.exclude)
m5 <- lm(as.formula(paste("tplm_rescaled ~ condition +", covariates)), data = subset(df, Race == "1"),  na.action = na.exclude)
m6 <- lm(as.formula(paste("tplm_rescaled ~ condition +", covariates)), data = subset(df, Race == "3"),  na.action = na.exclude)


# Table 5 (No Covariates shown)
stargazer(list(m4, m5, m6), 
          omit = c("Southern.Identity_rescaled", "Age", "State_name","Party_ID", "Education", "rr_index_rescaled", "Citizen","Region", "Gender","community_confederate"),
          covariate.labels = c("Treatment", "Constant"),
          column.labels = c("White", "Black", "Latino"),
          omit.stat = c("rsq", "f"), 
          df = FALSE, 
          align = TRUE, 
          digits = 2, 
          no.space = TRUE,
          title = "Average Treatment Effect by Race on Perceptions of Procedural Justice")

# Table 12 (Covariates shown) Appendix
stargazer(list(m4, m5, m6), 
          covariate.labels = c("Treatment", "Southern Identity", "Age", "Male", "Other Gender",
                               "State: AR", "State: FL", "State: GA", "State: LA",
                               "State: MS", "State: NC", "State: SC", "State: TN",
                               "State: TX", "State: VA", "Party ID",
                               "Rural", "Small Suburb", "Small Town", "Urban",
                               "Education", "Racial Resentment Index",
                               "Naturalized Citizen", "Non-Citizen",
                               "Confederate Monument in Community", "Constant"),
          column.labels = c("White", "Black", "Latino"),
          omit.stat = c("rsq", "f"), 
          df = FALSE, 
          align = TRUE, 
          digits = 2, 
          no.space = TRUE,
          title = "Average Treatment Effect by Race on Perceptions of Procedural Justice: Controls",
          out = "~/Dropbox/Apps/Overleaf/Confederate Monuments/table3_controls.tex")


```

# Table Resentment Models

```{r, warning=FALSE}
#Racial Resentment Tables

covariates <- "Southern.Identity_rescaled  + Age + Gender + State_name + Party_ID + Region +
Education + Citizen + community_confederate"

# Table 6 (No Covariates shown)
m1 <- lm(as.formula(paste("open_court_scale_rescaled ~ condition * rr_index_rescaled +", covariates)), data = subset(df, Race == "2"),  na.action = na.exclude)
m2 <- lm(as.formula(paste("tplm_rescaled ~ condition * rr_index_rescaled +", covariates)), data = subset(df, Race == "2"),  na.action = na.exclude)

m3 <- lm(as.formula(paste("open_court_scale_rescaled ~ condition * rr_index_rescaled +", covariates)), data = subset(df, Race == "3"),  na.action = na.exclude)
m4 <- lm(as.formula(paste("tplm_rescaled ~ condition * rr_index_rescaled +", covariates)), data = subset(df, Race == "3"),  na.action = na.exclude)

          
stargazer(list(m1,m2,m3,m4), 
               omit = c("Southern.Identity_rescaled", "Age", "State_name","Party_ID", "Education", "Citizen","Region", "Gender","community_confederate"),
          #omit.labels = c("State fixed effects","Southern Identity"),
          covariate.labels = c("Treatment", "Racial Resentment", "Treatment x Racial Resentment", "Constant"),
          omit.stat = c("rsq", "f"), 
          df = FALSE, 
          align = TRUE, 
          digits = 2, 
          no.space = TRUE,
          title = "Effect of Confederate Monument by Racial Resentment",
          dep.var.caption = "Courthouse Court System"
)

# Table 13 (Covariates shown) Appendix          
stargazer(list(m1,m2,m3,m4), 
          covariate.labels = c("Treatment", "Racial Resentment Index", "Southern Identity", "Age", "Male", "Other Gender",
                               "State: AR", "State: FL", "State: GA", "State: LA",
                               "State: MS", "State: NC", "State: SC", "State: TN",
                               "State: TX", "State: VA", "Party ID",
                               "Rural", "Small Suburb", "Small Town", "Urban",
                               "Education",   "Naturalized Citizen", "Non-Citizen",
                               "Confederate Monument in Community", "Racial Resentment X Treatment", "Constant"),
          column.labels = c("White", "Black", "Latino"),
          omit.stat = c("rsq", "f"), 
          df = FALSE, 
          align = TRUE, 
          digits = 2, 
          no.space = TRUE,
          title = "Effect of Confederate Monument by Racial Resentment",
          out = "~/Dropbox/Apps/Overleaf/Confederate Monuments/table4_controls.tex")


```


# Figure: Racial Resentment Moderators

```{r, warning=FALSE}

custom_theme <- theme(
  plot.title = element_text(size = 24, face = "bold"),
  axis.title.x = element_text(size = 20),
  axis.title.y = element_text(size = 20),
  axis.text.y = element_text(size = 20),
  legend.text = element_text(size = 20),
  legend.title = element_text(size = 20),
  axis.text.x = element_text(size = 20)
)

# Figure 2
rr1 <- plot_model(m1, type = "pred", terms = c("rr_index_rescaled", "condition"), auto.label = TRUE, title ="White Respondents") + 
  theme(legend.position = "left") + 
  custom_theme + 
  labs(x = "Racial Resentment", y = "Open Court")


rr2 <- plot_model(m2, type = "pred", terms = c("rr_index_rescaled", "condition"), auto.label = TRUE, title ="Latino Respondents") + 
  theme(legend.position = "none") + 
  custom_theme + 
  labs(x = "Racial Resentment", y = "Open Court")


rr_int <- grid.arrange(rr1, rr2, ncol = 2) 
rr_int

path_to_save <- "~/Dropbox/Apps/Overleaf/Confederate Monuments/rr_int.png"
ggsave(filename = path_to_save, plot = rr_int, width = 15, height = 7, dpi = 300)

# Figure 3
rr3 <- plot_model(m3, type = "pred", terms = c("rr_index_rescaled", "condition"), auto.label = TRUE, title ="White Respondents") + 
  theme(legend.position = "left") + 
  custom_theme + 
  labs(x = "Racial Resentment", y = "Procedural Justice")


rr4 <- plot_model(m4, type = "pred", terms = c("rr_index_rescaled", "condition"), auto.label = TRUE, title ="Latino Respondents") + 
  theme(legend.position = "none") + 
  custom_theme + 
  labs(x = "Racial Resentment", y = "Procedural Justice")

rr_int2 <- grid.arrange(rr3, rr4, ncol = 2) 
rr_int2

path_to_save <- "~/Dropbox/Apps/Overleaf/Confederate Monuments/rr_int2.png"
ggsave(filename = path_to_save, plot = rr_int2, width = 15, height = 7, dpi = 300)


```

# Table and Figure: Courthouse serves all ATE

```{r, warning=FALSE}
#Recode to 0 /1 and get complete cases
df$treatment[df$condition == "control"] <- 0
df$treatment[df$condition == "treatment"] <- 1
df_com <- df[complete.cases(df$ServeCourtsQ), ]
df_com$treatment <- as.numeric(df_com$treatment)
df_com$ServeCourtsQ <- as.numeric(df_com$ServeCourtsQ)


m1 <- glm(ServeCourtsQ ~ condition + Southern.Identity_rescaled  + Age + Gender + State_name + Party_ID + Region + rr_index_rescaled +
Education + Citizen + community_confederate, family= "binomial", data = subset(df_com, Race == "2")) #White
m2 <- glm(ServeCourtsQ ~ condition + Southern.Identity_rescaled  + Age + Gender + State_name + Party_ID + Region + rr_index_rescaled+
Education + Citizen + community_confederate , family= "binomial", data = subset(df_com, Race == "1")) # Black
m3 <- glm(ServeCourtsQ ~ condition + Southern.Identity_rescaled  + Age + Gender + State_name + Party_ID + Region + rr_index_rescaled+
Education + Citizen + community_confederate, family= "binomial", data = subset(df_com, Race == "3")) #Latino

# Table 6 (show no covariates)
stargazer(list(m1,m2,m3), 
          omit = c("Southern.Identity_rescaled", "Age", "State_name","Party_ID", "Education", "Citizen","Region", "Gender","community_confederate", "rr_index_rescaled"),
        #  omit.labels = c("State fixed effects"),
          covariate.labels = c("Treatment", "Constant"),
          column.labels = c("White", "Black", "Latino"),
          omit.stat = c("rsq", "f"), 
          df = FALSE, 
          align = TRUE, 
          digits = 2, 
          no.space = TRUE,
          title = "Effect of Confederate Monument in Front of Courthouse by Race",
          dep.var.caption = "Courthouse Serves All"
)


```

```{r, warning=FALSE}

# Figure 4
calc_diff_means <- function(data, race) {
  subset_data <- subset(data, Race == race)
  mean_control <- mean(subset_data$ServeCourtsQ[subset_data$condition == "control"], na.rm = TRUE)
  mean_treatment <- mean(subset_data$ServeCourtsQ[subset_data$condition == "treatment"], na.rm = TRUE)
  difference_in_means <- mean_treatment - mean_control
  return(data.frame(Race = race, Mean_Control = mean_control, Mean_Treatment = mean_treatment, Difference = difference_in_means))
}

# Apply the function for each racial group
diff_means_white <- calc_diff_means(df_com, race = "2")  # White
diff_means_black <- calc_diff_means(df_com, race = "1")  # Black
diff_means_latino <- calc_diff_means(df_com, race = "3") # Latino

# Combine results into one data frame
diff_means <- rbind(diff_means_white, diff_means_black, diff_means_latino)
print(diff_means)

# Calculate the y-axis range
y_range <- range(df_com$ServeCourtsQ, na.rm = TRUE)

# Helper function to calculate means and create labels
calculate_means <- function(data, race) {
  subset_data <- subset(data, Race == race)
  means <- aggregate(ServeCourtsQ ~ condition, data = subset_data, FUN = mean, na.rm = TRUE)
  means$label <- round(means$ServeCourtsQ, 2)  # Round the means for labels
  return(means)
}

# Calculate means for each race
means_white <- calculate_means(df_com, race = "2")
means_black <- calculate_means(df_com, race = "1")
means_latino <- calculate_means(df_com, race = "3")

# Create bar plots with proportion labels
plot_white <- ggplot(subset(df_com, Race == "2"), aes(x = condition, y = ServeCourtsQ)) +
  stat_summary(fun = "mean", geom = "bar", fill = "skyblue", color = "black") +
  geom_text(data = means_white, aes(x = condition, y = ServeCourtsQ, label = label), vjust = -0.5) +
  labs(title = "White", x = "", y = "Proportion") +
  ylim(y_range)

plot_black <- ggplot(subset(df_com, Race == "1"), aes(x = condition, y = ServeCourtsQ)) +
  stat_summary(fun = "mean", geom = "bar", fill = "lightcoral", color = "black") +
  geom_text(data = means_black, aes(x = condition, y = ServeCourtsQ, label = label), vjust = -0.5) +
  labs(title = "Black", x = "", y = "") +
  ylim(y_range)

plot_latino <- ggplot(subset(df_com, Race == "3"), aes(x = condition, y = ServeCourtsQ)) +
  stat_summary(fun = "mean", geom = "bar", fill = "goldenrod", color = "black") +
  geom_text(data = means_latino, aes(x = condition, y = ServeCourtsQ, label = label), vjust = -0.5) +
  labs(title = "Latino", x = "", y = "") +
  ylim(y_range)

# Arrange the plots in one grid
serves_all <- grid.arrange(plot_white, plot_black, plot_latino, ncol = 3)

path_to_save <- "~/Dropbox/Apps/Overleaf/Confederate Monuments/serves_all.png"
ggsave(filename = path_to_save, plot = serves_all, width = 10, height = 5, dpi = 300)

```


# APPENDIX 
# Descriptive Statistics Table

```{r}

table_vars <- list(
  Race_full = "Race/Ethnicity",
  Gender = "Gender",
  State_name = "State",
  Age_Full = "Age",
  Education_full = "Education",
  Region = "Region",
  Citizen = "Citizen",
  Exposure = "Exposure",
  community_confederate= "Confederate Monument in Community",
  Party_ID_full = "Party"
)

# Create a list of tables dynamically with column percentages
table_list <- lapply(names(table_vars), function(var) {
  df %>%
    tbl_cross(
      row = !!sym(var),
      col = condition,
      percent = "col" # Set percent to "col" for column percentages
    ) %>%
    add_p()
})

# Stack the tables
final_table <- tbl_stack(tbls = table_list)

# Convert the table to a LaTeX-friendly format
latex_code <- final_table %>%
  as_gt() %>%
  gt::as_latex()

cat(latex_code)

```

# Table: Balance Tests: Continuous Variables

```{r}
covariates <- c("rr_index_rescaled", "Southern.Identity")

# Function to summarize and test balance for each covariate
balance_test <- function(data, covariate) {
  data %>%
    summarise(
      Variable = covariate,
      Mean_Control = mean(data %>% filter(condition == "control") %>% pull(!!sym(covariate)), na.rm = TRUE),
      SD_Control = sd(data %>% filter(condition == "control") %>% pull(!!sym(covariate)), na.rm = TRUE),
      Mean_Treatment = mean(data %>% filter(condition == "treatment") %>% pull(!!sym(covariate)), na.rm = TRUE),
      SD_Treatment = sd(data %>% filter(condition == "treatment") %>% pull(!!sym(covariate)), na.rm = TRUE),
      P_Value = t.test(
        data %>% filter(condition == "control") %>% pull(!!sym(covariate)),
        data %>% filter(condition == "treatment") %>% pull(!!sym(covariate))
      )$p.value
    )
}

# Apply the function to each covariate
results <- lapply(covariates, function(x) balance_test(df, x))

# Combine results into a single data frame
results_df <- bind_rows(results)

# Print the results
print(results_df)

results_df_long <- results_df %>%
  mutate(
    Control = paste0(round(Mean_Control, 3), " (", round(SD_Control, 3), ")"),
    Treatment = paste0(round(Mean_Treatment, 3), " (", round(SD_Treatment, 3), ")")
  ) %>%
  select(Variable, Control, Treatment, P_Value)

# Export results_df to LaTeX table
results_df_long %>%
  kbl(
    caption = "Balance Tests for Continuous Covariates",
    format = "latex",
    col.names = c("Variable", "Control (Mean ± SD)", "Treatment (Mean ± SD)", "P-Value"),
    align = c("l", "c", "c", "c"),
    booktabs = TRUE
  ) %>%
  kable_styling(
    latex_options = c("hold_position")
  ) %>%
  save_kable("balance_tests_results_.tex")

```



